package cn.diancan.action.manager;


import cn.diancan.action.BaseAction;
import cn.diancan.exception.OutMsgException;
import cn.diancan.manager.security.ManagerAuth;
import cn.diancan.manager.security.ManagerAuthUtil;
import cn.diancan.model.SysManager;
import cn.diancan.model.SysRole;
import cn.diancan.util.MD5;
import cn.diancan.vo.ManagerVo;
import com.opensymphony.xwork2.ModelDriven;

import java.util.HashSet;
import java.util.List;

public class ManagerAction extends BaseAction implements ModelDriven<ManagerVo> {
    
    private static final long serialVersionUID = 1L; 
    private ManagerVo requestVo = new ManagerVo();

    /**
     * 管理员列表
     */
    @ManagerAuth
    public String managerList() {
	List<SysManager> list = sysManagerService.findByAllManager();
	getRequest().setAttribute("list", list);
	return freemarkView("/views/ftl/manager/manager/manager_list.ftl");
    }
	
    /**
     * 新增管理员界面
     */
    @ManagerAuth
    public String addManagerUI() {
	return freemarkView("/views/ftl/manager/manager/manager_add.ftl");

    }
	
    /**
     * 新增管理员
     */
    @ManagerAuth
    public String addManager() {
	try {
	    SysManager bean = new SysManager();
	    bean.setPassWd(MD5.createPassword(requestVo.getPassWd()));
	    bean.setPhoneNum(requestVo.getPhoneNum());
	    bean.setUserName(requestVo.getUserName());
	    sysManagerService.saveMangerAccount(bean);
	} catch (OutMsgException e) {
	    e.printStackTrace();
	}
	return redirect("/manager/Manager_managerList");

    }
	
    /**
     * 编辑管理员界面
     */
    @ManagerAuth
    public String editManagerUI() {
	try {
	    SysManager bean = sysManagerService.findByManagerId(requestVo
		    .getId());
	    getRequest().setAttribute("manager", bean);
	} catch (OutMsgException e) {
	    e.printStackTrace();
	}
	return freemarkView("/views/ftl/manager/manager/manager_edit.ftl");
    }
	
    /**
     * 编辑管理员
     */
    @ManagerAuth
    public String editManager() {
	SysManager bean;
	try {
	    bean = sysManagerService.findByManagerId(requestVo.getId());
	    // 默认6个星号，不填写即不修改
	    if (requestVo.getPassWd() != null
		    && !"******".equals(requestVo.getPassWd())
		    && !"".equals(requestVo.getPassWd().trim())) {
		bean.setPassWd(MD5.createPassword(requestVo.getPassWd()));
	    }
	    bean.setPhoneNum(requestVo.getPhoneNum());
	    sysManagerService.updateManager(bean);
	} catch ( Exception e) {
	    e.printStackTrace();
	}
	return redirect("/manager/Manager_managerList");

    }
	
    /**
     * 启用或禁用管理员
     */
    @ManagerAuth
    public String deleteManager() {
		try {
			if (sysManagerService.updateIsDelete(requestVo.getId(),requestVo.getIs())) {
                if (0 == requestVo.getIs()) {
                return jsonSuccess("启用成功");
                }
                return jsonSuccess("停用成功");
            }
			return jsonError("操作失败");
		} catch (Exception e) {
			return jsonError(e);
		}
	}
	
    /**
     * 给管理员设置角色页面
     */
    @ManagerAuth
    public String setRoleUI() {
	try {
	    SysManager bManager = sysManagerService.findByManagerId(requestVo.getId());
	    String resourceIdList = "";
	    if (null != bManager.getRoles()) {
		for (SysRole bean : bManager.getRoles()) {
		    resourceIdList += bean.getId().toString() + ",";
		}
	    }
	    List<SysRole> list = sysRoleService.findAll();
	    getRequest().setAttribute("list", list);
	    getRequest().setAttribute("resourceIdList", resourceIdList);
	    getRequest().setAttribute("id", requestVo.getId());
	} catch (OutMsgException e) {
	    e.printStackTrace();
	} 
	return freemarkView("/views/ftl/manager/manager/manager_editRole.ftl"); 
    }
	
    /**
     * 给管理员设置角色
     * 
     * @throws OutMsgException
     */
    @ManagerAuth
    public String setRole() throws OutMsgException {
	SysManager bManager = sysManagerService.findByManagerId(requestVo.getId());
	bManager.setRoles(new HashSet<SysRole>(sysRoleService.findByIds(requestVo.getIds())));
	Boolean result = sysManagerService.saveMangerRole(bManager);
	return jsonFormat(result, "设置成功", "设置失败");
    }
	
    /**
     * 管理员登出
     * 
     * @return
     * @throws Exception
     */
    @ManagerAuth
    public String loginOut() throws Exception {
	try {
	    ManagerAuthUtil.loginOut();
	    return jsonSuccess("成功退出登录");
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return jsonSuccess("退出登录失败");
    }

    @Override
    public ManagerVo getModel() {
	return requestVo;
    }
	
	
}
